<?php

namespace App\Model;


use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class UserBlockLog extends BaseModel
{
    protected $table = 'user_block_log';
    public $timestamps = false;
    protected $fillable = ['user_id', 'uuid', 'operation_id', 'operate_type', 'operate_info', 'operate_time', 'block_time', 'forbid_type', 'reason_id', 'reason_img'];


    const FORBID_DURATION = [
        ['1小时', 3600],
        ['3小时', 3600 * 3],
        ['12小时', 3600 * 12],
        ['24小时', 3600 * 24],
        ['48小时', 3600 * 24 * 2],
        ['7天', 3600 * 24 * 7],
        ['15天', 3600 * 24 * 15],
        //        ['1个月', 3600 * 24 * 30],
        ['2个月', 3600 * 24 * 30 * 2],
        ['3个月', 3600 * 24 * 30 * 3],
        ['6个月', 3600 * 24 * 30 * 6],
        ['永久', '0']
    ];

    public function getList(Request $request, $user_id)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $page = ($page - 1) * $limit;
        $uuid = $request->input('uuid');
        $nickname = $request->input('nickname');


        $where = " AND u_b.user_id IN(" . implode(',', $user_id) . ")";
        if ($uuid)
            $where .= ' AND u.uuid=' . $uuid;
        if ($nickname)
            $where .= " AND u.nickname LIKE '{$nickname}%'";

        $total = DB::connection('api')->selectOne("SELECT COUNT(*) total FROM (SELECT
                MAX(id) id
            FROM
                zb_user_block_log u_b
            INNER JOIN zb_user u ON u_b.user_id=u.user_id
            WHERE
                1 {$where}
            GROUP BY
                u_b.user_id) u");

        $sql = "SELECT
                u_b.id,
                u_b.user_id,
                u_b.operate_info,
                FROM_UNIXTIME(u_b.operate_time,'%Y-%m-%d %H:%i:%s') operate_time,
                IF(u_b.block_time >0,FROM_UNIXTIME(u_b.block_time,'%Y-%m-%d %H:%i:%s'), '永久') block_time,
                u.uuid,
                u.nickname,
                u_b.reason_id,
                u_b.reason_img
            FROM
                (select * from zb_user_block_log where id in (select max(id) from zb_user_block_log group by user_id)) u_b
            INNER JOIN zb_user u ON u_b.user_id=u.user_id
            WHERE
                1 {$where}
            ORDER BY
                operate_time DESC
            LIMIT {$page},{$limit}";
        $user_block_rule_model = new UserBlockRule();
        $user_block_rule = $user_block_rule_model->getRule();
        $user_block_rule = collect($user_block_rule)->groupBy('id')->toArray();
        $forbid_user_list = [];
        foreach (DB::connection('api')->cursor($sql) as $forbid) {
            $forbid = collect($forbid)->toArray();
            array_push($forbid_user_list, collect($forbid)->merge([
                'reason_p' => $user_block_rule[$user_block_rule[$forbid['reason_id']][0]['pid']][0]['name'] ?? '',
                'reason_img' => explode(',', $forbid['reason_img']) ?? [],
                'reason' => $user_block_rule[$forbid['reason_id']][0]['name'] ?? ''
            ]));
        }
        return ['data' => $forbid_user_list, 'total' => $total->total];
    }

    public function userBlockRule()
    {
        return $this->belongsTo(UserBlockRule::class, 'reason_id', 'id')
            ->withDefault();
    }

    public function number()
    {
        return $this->belongsTo(Number::class, 'user_id', 'user_id')
            ->withDefault();
    }

    public function users()
    {
        return $this->belongsTo(Users::class, 'operation_id', 'id')
            ->withDefault(['name' => '空']);
    }



    //添加
    public function createData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/user/block/create', $data, 'json', 'POST');
    }

    //解封
    public function updateData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/user/block/update', $data, 'json', 'POST');
    }

    //封禁设备
    public function handleForbidDevice($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/user/forbid_device', $data, 'json', 'POST');
    }

    public function handleForbidIp($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/user/forbid_ip', $data, 'json', 'POST');
    }
    public function handleUnForbidIp($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/user/unforbid_ip', $data, 'json', 'POST');
    }
    public function handleDenyIpList()
    {
        return curl_request(getKkaudioApiUrl() . '/inner/user/deny_ip_list', [], 'json', 'POST');
    }
    public function remove_ip_forbid($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/user/remove_ip_forbid', $data, 'json', 'POST');
    }
}
